#include "lvgl.h"
#include "img_def.h"
#include "lv_common_function.h"
#include "fr_lv_customer_page.h"
#include "FreeRTOS.h"
#include "app_task.h"

//1001,mean:v1.0.0.1
#define     FW_VERSION      1001

extern void co_delay_100us(uint32_t count);
//=======================================================
LV_ATTRIBUTE_LARGE_CONST const ui_func style_func_arry[] = 
{

};
/*
void update_rt_timer_cb(lv_timer_t *t)
{
    if(lv_obj_is_valid(t->user_data))
    {
        lv_label_set_text_fmt(t->user_data,"%02d:%02d",fr_system_time_hour(),fr_system_time_minute());
    }
}
void common_right_top_little_time_event_cb(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *rt_time = lv_event_get_target(e);
    if(code == LV_EVENT_DELETE)
    {
        if(rt_time->user_data != NULL)
        {
            lv_timer_del(rt_time->user_data);
            rt_time->user_data = NULL;
        }
    }
}
lv_obj_t *common_right_top_little_time(lv_obj_t *parent,lv_point_t *top)
{
    lv_obj_t *rt_time = lv_label_create(parent);
    lv_obj_align(rt_time,LV_ALIGN_TOP_RIGHT,-30,0);
    lv_obj_set_style_text_color(rt_time,lv_color_make(0xc0,0xc0,0xc0),0);
    lv_label_set_text_fmt(rt_time,"%02d:%02d",fr_system_time_hour(),fr_system_time_minute());
    lv_obj_add_event_cb(rt_time,common_right_top_little_time_event_cb,LV_EVENT_DELETE,NULL);
    rt_time->user_data = lv_timer_create(update_rt_timer_cb,500,rt_time);
    return rt_time;
}
*/
//=====================================================================
//----------------- about time ----------------------------------------
//=====================================================================
time_rtc_t *fr_system_get_time_rtc(void)
{
    return &sync_cm3_data.time;
}
uint8_t fr_system_get_time_week(void)
{
    return CalcWeekday(sync_cm3_data.time);
}
uint8_t fr_system_get_time_hour(void)
{
    uint8_t cur_Hour;
	if(sync_cm3_data.ui_info.time_format)
    {
        if(sync_cm3_data.time.ucHour < 12)
        {
            if(sync_cm3_data.time.ucHour == 0)
                cur_Hour = 12;
	        else
                cur_Hour = sync_cm3_data.time.ucHour % 12;
        }
        else
        {
            if(sync_cm3_data.time.ucHour == 12)
                cur_Hour = 12;
            else
                cur_Hour = sync_cm3_data.time.ucHour - 12;
        }
    }
    else
    {
        cur_Hour = sync_cm3_data.time.ucHour;
    }
    return cur_Hour;
}
uint8_t fr_system_get_time_minute(void)
{
    return sync_cm3_data.time.ucMinute;
}
uint8_t fr_system_get_time_second(void)
{
    return sync_cm3_data.time.ucSecond;
}
void fr_system_set_time_hour(uint8_t temp_data)
{
    sync_cm3_data.time.ucHour = temp_data;
    fr_system_update_utc_time(&sync_cm3_data.time);
}
void fr_system_set_time_minute(uint8_t temp_data)
{
    sync_cm3_data.time.ucMinute = temp_data;
    fr_system_update_utc_time(&sync_cm3_data.time);
}
void fr_system_set_time_second(uint8_t temp_data)
{
    sync_cm3_data.time.ucSecond = temp_data;
    fr_system_update_utc_time(&sync_cm3_data.time);
}
void fr_system_set_time_format(uint8_t temp_data)
{
    //0:24 hour,1:12 hour
    sync_cm3_data.ui_info.time_format = temp_data;
}
void fr_system_timer_delay(uint16_t n)
{
    co_delay_100us(n);
}
//----------------------- end -----------------------------------------

//=====================================================================
//----------------- about watch setting--------------------------------
//=====================================================================
//cm3 fw version
uint16_t fr_system_get_fw_version(void)
{
    // return sync_cm3_data.m3_version_new;
    return (uint16_t)FW_VERSION;
}
//MAC address
uint8_t *fr_system_get_mac_address(void)
{
    return sync_cm3_data.ble_address;
}
void fr_system_set_Wirst_Awake(uint8_t state)
{
    uint8_t data = state;
    dsp_post_operate_to_m3(DSP_EVT_WIRST,&data,1);
}
uint8_t fr_system_Wirst_Awake(void)
{
    return sync_cm3_data.motion;
}
//no disturb
void fr_system_set_no_disturb( uint8_t state)
{
    uint8_t data = state;
    dsp_post_operate_to_m3(DSP_EVT_DISTURB_CONTROL,&data,1);
}

uint8_t fr_system_no_disturb(void)
{
    return sync_cm3_data.disturb_state;
}

//panel power on time
void fr_system_set_panel_on_time( uint8_t value)
{

}
uint8_t fr_system_panel_on_time(void)
{
    return 0;
}

//language
void fr_system_set_language( uint8_t id)
{
    sync_cm3_data.ui_info.ui_language = id;
	// dsp_ipc_cmd = M3_DRIVER_DAILY_DATA;
	//ipc_msg_with_payload_send(IPC_MSG_M3_INPUT, &dsp_ipc_cmd, 1, (uint8_t *)&sync_cm3_data, sizeof(ipc_daily_data_t), NULL);

}

uint8_t fr_system_get_language(void)
{
    return sync_cm3_data.ui_info.ui_language;
}

//backlight
void fr_system_set_backlight( uint8_t id)
{
    uint8_t data = id;
    sync_cm3_data.ui_info.ui_backlight_brightness = data;
    dsp_post_operate_to_m3(DSP_EVT_BRIGHT,&data,1);
}
uint8_t fr_system_get_backlight(void)
{
    return sync_cm3_data.ui_info.ui_backlight_brightness;
}

//ble state
uint8_t fr_system_get_ble_state(void)
{
    return sync_cm3_data.ble_state;
}
//bt state
uint8_t fr_system_get_bt_state(void)
{
    return sync_cm3_data.bt_state;
}
//baterry
uint8_t fr_system_get_battery_state(void)
{
    return sync_cm3_data.battery.state;
}
uint8_t fr_system_get_battery_value(void)
{
    return sync_cm3_data.battery.battery_value;
}

uint8_t *fr_system_get_sleep_distory(void)
{
    return NULL;
}
uint16_t fr_system_get_sleep_total_time(void)
{
    return sync_cm3_data.sleep_total_time;
}

uint16_t fr_system_get_sleep_d_time(void)
{
    return sync_cm3_data.deep_sleep_time;
}

uint16_t fr_system_get_sleep_l_time(void)
{
    return sync_cm3_data.light_sleep_time;;
}
uint8_t fr_system_get_day1_weather_type(void)
{
    if(sync_cm3_data.day1.type > 9)
    {
        return 0;
    }
    else
        return sync_cm3_data.day1.type;
}
void fr_system_set_weather_temperature_format(uint8_t temp_data)
{
    sync_cm3_data.ui_info.temperature_format = temp_data;
}
uint8_t fr_system_get_weather_temperature_format(void)
{
    return sync_cm3_data.ui_info.temperature_format;
}
int16_t centigrade2fahrenhite(int16_t centigrade_data)
{
    return (centigrade_data*18+320)/10;
}
int16_t fr_system_get_day1_weather_temp(void)
{
	if(sync_cm3_data.ui_info.temperature_format)
		return centigrade2fahrenhite(sync_cm3_data.day1.cur_tempture);
	else
    	return sync_cm3_data.day1.cur_tempture;
}
int16_t fr_system_get_day1_weather_temp_m(void)
{
	if(sync_cm3_data.ui_info.temperature_format)
		return centigrade2fahrenhite(sync_cm3_data.day1.h_tempture);
	else
    	return sync_cm3_data.day1.h_tempture;
}

int16_t fr_system_get_day1_weather_temp_n(void)
{
	if(sync_cm3_data.ui_info.temperature_format)
		return centigrade2fahrenhite(sync_cm3_data.day1.l_tempture);
	else
    	return sync_cm3_data.day1.l_tempture;
}


uint8_t fr_system_get_day2_weather_type(void)
{
    if(sync_cm3_data.day2.type > 9)
    {
        return 0;
    }
    else
        return sync_cm3_data.day2.type;
}

int16_t fr_system_get_day2_weather_temp(void)
{
	if(sync_cm3_data.ui_info.temperature_format)
		return centigrade2fahrenhite(sync_cm3_data.day2.cur_tempture);
	else
    return sync_cm3_data.day2.cur_tempture;
}
int16_t fr_system_get_day2_weather_temp_m(void)
{
	if(sync_cm3_data.ui_info.temperature_format)
		return centigrade2fahrenhite(sync_cm3_data.day2.h_tempture);
	else
    	return sync_cm3_data.day2.h_tempture;
}

int16_t fr_system_get_day2_weather_temp_n(void)
{
	if(sync_cm3_data.ui_info.temperature_format)
		return centigrade2fahrenhite(sync_cm3_data.day2.l_tempture);
	else
    	return sync_cm3_data.day2.l_tempture;
}
uint8_t fr_system_get_day3_weather_type(void)
{
    if(sync_cm3_data.day3.type > 9)
    {
        return 0;
    }
    else
        return sync_cm3_data.day3.type;
}

int16_t fr_system_get_day3_weather_temp(void)
{
	if(sync_cm3_data.ui_info.temperature_format)
		return centigrade2fahrenhite(sync_cm3_data.day3.cur_tempture);
	else
    	return sync_cm3_data.day3.cur_tempture;
}
int16_t fr_system_get_day3_weather_temp_m(void)
{
	if(sync_cm3_data.ui_info.temperature_format)
		return centigrade2fahrenhite(sync_cm3_data.day3.h_tempture);
	else
    	return sync_cm3_data.day3.h_tempture;
}

int16_t fr_system_get_day3_weather_temp_n(void)
{
	if(sync_cm3_data.ui_info.temperature_format)
		return centigrade2fahrenhite(sync_cm3_data.day3.l_tempture);
	else
    	return sync_cm3_data.day3.l_tempture;
}
//sport
uint32_t fr_system_get_sport_time(void)
{
    return sync_cm3_data.sport.sport_time;
}
uint8_t fr_system_get_sport_type(void)
{
    return sync_cm3_data.sport.sport_type;
}

uint16_t fr_system_get_sport_speed(void)
{
    return sync_cm3_data.sport.speed;
}

uint16_t fr_system_get_sport_step(void)
{
    return sync_cm3_data.sport.step;
}

uint16_t fr_system_get_sport_carlorie(void)
{
    return sync_cm3_data.sport.carloie;
}

uint16_t fr_system_get_sport_distcace(void)
{
	if(fr_system_get_distance_format())
		return sync_cm3_data.sport.distance*621/1000;
	else
    	return sync_cm3_data.sport.distance;
}
uint8_t fr_system_get_sport_state(void)
{
    return sync_cm3_data.sport.state;
}

uint8_t fr_system_get_sport_hrm(void)
{
    return sync_cm3_data.sport.hrm;
}

uint8_t fr_system_get_sport_hrm_max(void)
{
    return sync_cm3_data.sport.max_hrm;
}

uint8_t fr_system_get_sport_hrm_min(void)
{
    return sync_cm3_data.sport.min_hrm;
}
uint8_t fr_system_get_sport_oxygen(void)
{
    return sync_cm3_data.sport.oxygen;
}
uint32_t fr_system_get_sport_total_time(void)
{
    return sync_cm3_data.sport.sport_total_time;
}
uint16_t fr_system_get_sport_total_carlorie(void)
{
    return sync_cm3_data.sport.total_carloie;
}
uint8_t fr_system_get_sport_avg_hrm(void)
{
    return sync_cm3_data.sport.avg_hrm;
}
void fr_system_sports_start(uint8_t mode)
{
    uint8_t buffer[2];
    buffer[0] = 1;  //start
    buffer[1] = mode;
    dsp_post_operate_to_m3(DSP_EVT_SPORTS,buffer,2);   
    sync_cm3_data.sport.sport_type =  mode;

}

void fr_system_sports_stop(uint8_t mode)
{
    uint8_t buffer[2];
    buffer[0] = 0;  //stop
    buffer[1] = mode;
    dsp_post_operate_to_m3(DSP_EVT_SPORTS,buffer,2);  
}

void fr_system_sports_pause(uint8_t mode)
{
    uint8_t buffer[2];
    buffer[0] = 2;  //pause
    buffer[1] = mode;
    dsp_post_operate_to_m3(DSP_EVT_SPORTS,buffer,2);  
}

void fr_system_turn_on_bt(void)
{
    uint8_t data = 1;
    dsp_post_operate_to_m3(DSP_EVT_BT_SWATCH,&data,1);
    fr_system_timer_delay(100);
}
void fr_system_turn_off_bt(void)
{
    uint8_t data = 0;
    dsp_post_operate_to_m3(DSP_EVT_BT_SWATCH,&data,1);
    fr_system_timer_delay(100);
}
void fr_system_shut_down(void)
{
    dsp_post_operate_to_m3(DSP_EVT_SYSTEM_SHUTDOWN,NULL,0);
}
void fr_system_reset_to_factory(void)
{
    //just test
    extern void system_reset(void); 
    system_reset();
}
void fr_system_reboot(void)
{
    // dsp_post_operate_to_m3(DSP_EVT_SYSTEM_REBOOT,NULL,0);
    extern void system_reset(void); 
    system_reset();
}
void fr_system_power_off(void)
{
    //just test
    extern void system_reset(void); 
    system_reset();
}
void fr_system_enable_mic(void)
{
    sync_cm3_data.audio_mic_on = 1;
    dsp_post_operate_to_m3(DSP_EVT_MIC_OPEN,NULL,0);
}

void fr_system_disable_mic(void)
{
    sync_cm3_data.audio_mic_on = 0;
    dsp_post_operate_to_m3(DSP_EVT_MIC_MUTE,NULL,0);

}
uint8_t fr_system_get_mic_state(void)
{
    return sync_cm3_data.audio_mic_on;
}
void fr_system_set_step_target(uint32_t temp_data)
{
    sync_cm3_data.steptarget = temp_data;
}
uint32_t fr_system_get_step_target(void)
{
    return sync_cm3_data.steptarget;
}
uint32_t fr_system_get_step(void)
{
    return sync_cm3_data.step;
}
uint32_t fr_system_get_calorie(void)
{
    return sync_cm3_data.calorie;
}
uint8_t fr_system_get_distance_format(void)
{	
	return sync_cm3_data.ui_info.distance_format;
}
uint32_t fr_system_get_distance(void)
{
    return sync_cm3_data.distance;
}

void fr_system_clear_light_time(void)
{
    extern uint32_t gui_running_last_time;
    gui_running_last_time = portGET_RUN_TIME_COUNTER_VALUE();
}

void fr_system_take_picture(void)
{
    dsp_post_operate_to_m3(DSP_EVT_CAMERA,NULL,1);
}
void fr_system_search_phone(void)
{
	uint8_t data = 0x01;
	dsp_post_operate_to_m3(DSP_EVT_FIND,&data,1);
}

void fr_system_set_menu_style(uint8_t id)
{
    sync_cm3_data.ui_info.ui_menu_style_id = id; 
}
uint8_t fr_system_get_menu_style(void)
{
    return sync_cm3_data.ui_info.ui_menu_style_id;
}
void fr_system_set_watch_face( uint8_t id)
{
    sync_cm3_data.ui_info.ui_clock_id = id;
}
uint8_t fr_system_get_watch_face(void)
{
    return sync_cm3_data.ui_info.ui_clock_id;
}
uint8_t* fr_system_get_custom_page_mode(void)
{
    return sync_cm3_data.ui_info.custom_page_mode;
}
void fr_system_set_custom_page_mode(void)
{ 
//    uint8_t *custom_page_data = fr_lv_get_custom_page_data();
 //   memcpy(sync_cm3_data.ui_info.custom_page_mode,custom_page_data,CUSTOM_ID_ARRY_NUMBER);
    // dsp_post_operate_to_m3(DSP_EVT_SET_CARD_INDEX,sync_cm3_data.ui_info.custom_page_mode,CUSTOM_ID_ARRY_NUMBER);   
}
//----------------------- end -----------------------------------------

//=====================================================================
//----------------- about call ----------------------------------------
//=====================================================================
/*
uint8_t call_mode;
uint8_t fr_system_get_call_mode(void)
{
    return call_mode;
}
void fr_system_set_call_mode(uint8_t mode)
{
    call_mode = mode;
}
*/
extern bool coming_call_ok;
extern bool call_flag;
void dsp_op_system_call_out(uint8_t * number,uint8_t len)
{
    coming_call_ok = true;
    call_flag = true;
    memset(call_context,0,200);
    memcpy(call_context,number,len);
    uint8_t * buffer = (uint8_t *)pvPortMalloc(len + 3);
    if(buffer != NULL)
    {
        buffer[0] = 'D';
        buffer[1] = 'A';
        memcpy(buffer+2, number, len+1);
        dsp_post_operate_to_m3(DSP_EVT_CALL_CONTROL,buffer,len+3);
        vPortFree(buffer);
    }
}

void dsp_op_get_current_call(call_info_t * call)
{
    if(call_context[0] > 0)
    {
        call->number_len = call_context[0];
        memset(call->call_namber,0,32);
        memcpy(call->call_namber,call_context+1,call->number_len);
	    call->name_len = call_context[call->number_len] + 1;
        memset(call->call_name,0,128);
        memcpy(call->call_name, call_context+call->number_len+1, call->name_len);
    }
}

uint8_t dsp_op_dial_from_book(uint16_t id)
{
    uint8_t buffer[2];
    buffer[0] = id >> 8;
    buffer[1] = id;
    //dsp_post_operate_to_m3(DSP_EVT_READ_PHONE_BOOK_LIST,buffer,2);    
    //co_delay_100us(500);
    if(call_list[0] > 0)
    {
        memcpy(call_context,&book_list[id][0],200);


        if(call_context[0] < 3)
        {
            return 0;
        }

        bool namber_is_ok = true;
        for(uint8_t i = 1; i <= call_context[0]; i++)
        {
            if((call_context[i] >= '0') && (call_context[i] <= '9'))
            {

            }
            else if(call_context[i] == '+')   //area number
            {

            }
            else
            {
                namber_is_ok = false;
                break;
            }
        }


        if(namber_is_ok)
        {
            coming_call_ok = true;
            call_flag = true;

            uint8_t * buffer = (uint8_t *)pvPortMalloc(call_context[0] + 3);
            if(buffer != NULL)
            {
                buffer[0] = 'D';
                buffer[1] = 'A';
                memcpy(buffer+2, call_context, call_context[0]+1);

                dsp_post_operate_to_m3(DSP_EVT_CALL_CONTROL,buffer,call_context[0]+3);

                vPortFree(buffer);
            }

            return 1;
        }
        return 0;

    }
    return 0;
}

bool get_phone_book_flag = false;
bool phone_book_is_bank_flag = true;
uint32_t phone_book_total = 0;
void dsp_op_get_call_from_book(call_info_t * call,uint16_t id)
{
    uint8_t buffer[2];
    buffer[0] = id >> 8;
    buffer[1] = id;
    dsp_post_operate_to_m3(DSP_EVT_READ_PHONE_BOOK_LIST,buffer,2);
    get_phone_book_flag = true;
    uint16_t counter = 0;
    while(get_phone_book_flag)
    {
        co_delay_100us(1);
        counter ++;
        if(counter > 1000)
            break;
    }

    if(call_list[0] > 0)
    {
        call->number_len = call_list[0];
        memset(call->call_namber,0,32);
        memcpy(call->call_namber,call_list+1,call->number_len);
	    call->name_len = call_list[call->number_len] + 1;
        memset(call->call_name,0,128);
        memcpy(call->call_name, call_list+call->number_len+1, call->name_len);   
    }
}
void dsp_op_get_phone_book_tatol(void)
{
    dsp_post_operate_to_m3(DSP_EVT_READ_PHONE_BOOK_TOTAL,NULL,0);
    get_phone_book_flag = true;
    uint16_t counter = 0;
    while(get_phone_book_flag)
    {
        co_delay_100us(1);
        counter ++;
        if(counter > 1000)
            break;
    }
}

void dsp_op_sync_phone_book(void)
{
    dsp_post_operate_to_m3(DSP_EVT_LOAD_PHONE_BOOK,NULL,0);
}

uint16_t dsp_op_get_phone_book_id_offset(void)
{
    return sync_cm3_data.phone_book_id_offset;
}

uint16_t dsp_op_set_phone_book_id_offset(uint16_t id_offset)
{
    uint8_t buffer[2];
    buffer[0] = id_offset >> 8;
    buffer[1] = id_offset;
    dsp_post_operate_to_m3(DSP_EVT_SET_PHONE_BOOK_ID_OFFSET,buffer,2);
    //fr_system_timer_delay(2000);
    return 0;
}
void dsp_op_system_call_ok(void)
{
    coming_call_ok = true;
	uint8_t buffer[2];
	buffer[0] = 'C';
	buffer[1] = 'A';		
	//dsp_ipc_cmd = M3_DRIVER_CALLING;
	//ipc_msg_with_payload_send(IPC_MSG_M3_INPUT, &dsp_ipc_cmd,1,buffer,2,NULL);
    dsp_post_operate_to_m3(DSP_EVT_CALL_CONTROL,buffer,2);
}

void dsp_op_system_call_reject(void)
{
    call_flag = false;
    coming_call_ok = false;	
	uint8_t buffer[2];
	buffer[0] = 'C';
	buffer[1] = 'B';	
	//dsp_ipc_cmd = M3_DRIVER_CALLING;
	//ipc_msg_with_payload_send(IPC_MSG_M3_INPUT, &dsp_ipc_cmd,1,buffer,2,NULL);

    dsp_post_operate_to_m3(DSP_EVT_CALL_CONTROL,buffer,2);
}

void dsp_op_read_cm3_record_list(void)
{
	dsp_ipc_cmd = M3_DRIVER_CAllLIST_INFO;
	//(IPC_MSG_M3_INPUT, &dsp_ipc_cmd, 1, (uint8_t *)&sync_cm3_data, sizeof(ipc_daily_data_t), NULL);

    get_phone_book_flag = true;
    uint16_t counter = 0;
    while(get_phone_book_flag)
    {
        co_delay_100us(1);
        counter ++;
        if(counter > 1000)
            break;
    }
}

void dsp_op_get_call_from_record_list(call_info_t * call,uint8_t *call_context)
{
    memcpy(call_list,call_context,CALL_NUMBER_MAX_LEN - 1);        
    call->number_len = call_list[0];
    memset(call->call_namber,0,32);
    memcpy(call->call_namber,call_list+1,call->number_len);
    call->name_len = call_list[call->number_len] + 1;
    memset(call->call_name,0,128);
    memcpy(call->call_name, call_list+call->number_len+1, call->name_len);
}
uint8_t dsp_get_system_record_total(void)
{
    return record_list.call_record_total;
}
//----------------------- end -----------------------------------------

//=====================================================================
//----------------- about node ----------------------------------------
//=====================================================================
//layer_page
void dsp_store_ui_scene(uint8_t mode,uint8_t layer, uint8_t page,uint16_t page_cur_x,uint16_t page_cur_y)
{
    sync_cm3_data.ui_info.ui_mode = mode;
    sync_cm3_data.ui_info.ui_node_lv = layer;
    sync_cm3_data.ui_info.ui_page_id = page;
	dsp_ipc_cmd = M3_DRIVER_DAILY_DATA;
	//ipc_msg_with_payload_send(IPC_MSG_M3_INPUT, &dsp_ipc_cmd, 1, (uint8_t *)&sync_cm3_data, sizeof(ipc_daily_data_t), NULL);
}
void dsp_set_ui_scene_default_style(uint8_t type)
{
    sync_cm3_data.ui_info.ui_default_style = type;
}

uint8_t dsp_get_ui_scene_default_style(void)
{
    return sync_cm3_data.ui_info.ui_default_style;
}

uint8_t fr_system_panel_state(void)
{
    return sync_cm3_data.panel_state;
}

uint8_t dsp_get_ui_scene_mode(void)
{
    return sync_cm3_data.ui_info.ui_mode;
}
uint8_t dsp_get_ui_scene_page_id(void)
{
    return sync_cm3_data.ui_info.ui_page_id;    
}
//----------------------- end -----------------------------------------

//=====================================================================
//----------------- about host recv vendor-----------------------------
//=====================================================================
#define     RECEIVE_MODE_GSENSOR        0x0A
#define     RECEIVE_MODE_SPO2           0x0B
#define     RECEIVE_MODE_HRM            0x0C
typedef enum {
  STK_EVENT_NONE,
  STK_EVENT_STEP_NOTIFY,
  STK_EVENT_FALL_NOTIFY,
  STK_EVENT_SLEEP_NOTIFY,
  STK_EVENT_ACTION_NOTIFY,
  STK_EVENT_SEDENTARY_NOTIFY,
  STK_EVENT_CALORIE_NOTIFY,
  STK_EVENT_DISTANCE_NOTIFY,
  STK_EVENT_FLIP_NOTIFY,
  STK_EVENT_RAISE_NOTIFY,
  STK_EVENT_SHAKE_NOTIFY
}STKMOTION_EVENT;
typedef struct {
    STKMOTION_EVENT evt_id;
    signed int step;
    signed int calorie;
    signed char action;
    signed char sleep;
}_gsensor_evt;
typedef enum
{
    HRM_MODE = 1,
    SPO2_MODE,
}hrm_mode_t;
hrm_mode_t hrm_mode;

uint8_t system_hrm_status;      //1:open,0:close
uint8_t system_spo2_status;      //1:open,0:close


//---------------------------------------------------------------------
void fr_system_host_recv_vendor(uint8_t *recv_data,uint8_t len)
{
    uint8_t receive_mode = recv_data[1];
    switch (receive_mode)
    {
    case RECEIVE_MODE_GSENSOR:
        {
            _gsensor_evt *evt = (_gsensor_evt *)&recv_data[3];
            sync_cm3_data.step = evt->step;
            sync_cm3_data.calorie = evt->calorie;
            sync_cm3_data.motion = evt->action;
            printf("step:%d,calorie:%d,motion:%d\r\n",sync_cm3_data.step,
            sync_cm3_data.calorie,sync_cm3_data.motion);
        }
        break;
    case RECEIVE_MODE_HRM:
        system_hrm_status = recv_data[3];
        sync_cm3_data.bpm = recv_data[3+4+4];
        break;
    case RECEIVE_MODE_SPO2:
        system_spo2_status = recv_data[3];
        sync_cm3_data.spo2 = recv_data[3+4+4];
        printf("spo2 status:%d,spo2:%d\r\n",system_spo2_status,sync_cm3_data.spo2);
    default:
        break;
    }
}
//----------------------- end -----------------------------------------

//=====================================================================
//----------------- about hrm -----------------------------------------
//=====================================================================
void btdm_host_send_vendor_cmd(uint8_t type, uint8_t length, void *data);
uint8_t fr_system_get_hrm_status(void)
{
    return system_hrm_status;
}
void fr_system_hrm_start(void)
{
    uint8_t mode = 0;
    btdm_host_send_vendor_cmd(0x14, 1, &mode);

    system_hrm_status = 1;
    hrm_mode = HRM_MODE;
}
void fr_system_hrm_stop(void)
{
    btdm_host_send_vendor_cmd(0x15, 0, NULL);
    system_hrm_status = 0;
}
uint8_t fr_system_get_hrm(void)
{
    return sync_cm3_data.bpm;
}
uint8_t fr_system_get_hrm_warning_h(void)
{
    return sync_cm3_data.bpm_warning_h;
}
uint8_t fr_system_get_hrm_warning_l(void)
{
    return sync_cm3_data.bpm_warning_l;
}

//--------------------------------------------------
void fr_system_spo2_start(void)
{
    uint8_t mode = 1;
    btdm_host_send_vendor_cmd(0x14, 1, &mode);
    system_spo2_status = 1;
    hrm_mode = SPO2_MODE;
}
void fr_system_spo2_stop(void)
{
    btdm_host_send_vendor_cmd(0x15, 0, NULL);
    system_spo2_status = 0;
}
uint8_t fr_system_get_spo2(void)
{
    return sync_cm3_data.spo2;
}
uint8_t fr_system_get_spo2_status(void)
{
    return system_spo2_status;
}
//----------------------- end -----------------------------------------

//=====================================================================
//-------------------- about gsensor ----------------------------------
//=====================================================================
void fr_system_send_step_cmd(void)
{
    btdm_host_send_vendor_cmd(0x12,0,NULL);
}

void lv_img_big_disp(lv_obj_t* parent, const lv_img_dsc_t **src , lv_coord_t x, lv_coord_t y,uint8_t img_num,uint8_t start)
{
    lv_obj_t *cont1 = lv_obj_create(parent);
    lv_obj_remove_style_all(cont1);
    lv_obj_clear_flag(cont1, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_scrollbar_mode(cont1, LV_SCROLLBAR_MODE_OFF);
    //lv_obj_set_size(cont1,LV_PCT(100),LV_PCT(100));
    lv_obj_set_style_bg_color(cont1,lv_color_black(),0);
    lv_obj_set_size(cont1,LV_SIZE_CONTENT,LV_SIZE_CONTENT);
    lv_obj_align(cont1,LV_ALIGN_TOP_LEFT,x,y);
    lv_obj_t *old_obj = NULL;
    for (uint8_t i = 0; i < img_num; i++)
    {
        lv_obj_t *img1 = lv_img_create(cont1);
        lv_img_set_src(img1,src[start+i]);
        if(i == 0)
            lv_obj_set_pos(img1,0,0);
        else
            lv_obj_align_to(img1,old_obj,LV_ALIGN_OUT_BOTTOM_LEFT,0,0);
        old_obj = img1;
    }
    lv_obj_clear_flag(cont1,LV_OBJ_FLAG_CLICKABLE);
}

lv_obj_t* lv_img_big_create(lv_obj_t* parent, const lv_img_dsc_t **src , lv_coord_t x, lv_coord_t y,uint8_t img_num,uint8_t start)
{
    lv_obj_t *cont1 = lv_obj_create(parent);
    lv_obj_remove_style_all(cont1);
    lv_obj_clear_flag(cont1, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_scrollbar_mode(cont1, LV_SCROLLBAR_MODE_OFF);
    //lv_obj_set_size(cont1,LV_PCT(100),LV_PCT(100));
    lv_obj_set_style_bg_color(cont1,lv_color_black(),0);
    lv_obj_set_size(cont1,LV_SIZE_CONTENT,LV_SIZE_CONTENT);
    //lv_obj_align(cont1,LV_ALIGN_TOP_LEFT,x,y);
    lv_obj_t *old_obj = NULL;

    for(uint8_t i = 0; i < img_num; i++)
    {
        lv_obj_t *img1 = lv_img_create(cont1);
        lv_img_set_src(img1,src[start+i]);
        if(i == 0)
            lv_obj_set_pos(img1,0,0);
        else
            lv_obj_align_to(img1,old_obj,LV_ALIGN_OUT_BOTTOM_LEFT,0,0);
        old_obj = img1;
    }
    //lv_obj_clear_flag(cont1,LV_OBJ_FLAG_CLICKABLE);
    return cont1;

}

//----------------------- end -----------------------------------------
